package com.erp.pro.controller;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.erp.common.domain.DictDO;
import com.erp.common.service.DictService;
import com.erp.common.utils.ShiroUtils;
import com.erp.pro.domain.ProductQtyDO;
import com.erp.pro.service.ProductQtyService;
import com.erp.system.domain.UserDO;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.erp.pro.domain.WarehouseDO;
import com.erp.pro.service.WarehouseService;
import com.erp.common.utils.PageUtils;
import com.erp.common.utils.Query;
import com.erp.common.utils.R;

/**
 * 仓库管理
 * 
 * @author Eric
 * @email shiliang900713@163.com
 * @date 2019-06-29 13:23:56
 */
 
@Controller
@RequestMapping("/pro/warehouse")
public class WarehouseController {
	@Autowired
	private WarehouseService warehouseService;

	@Autowired
	private DictService dictService;

	@Autowired
	private ProductQtyService productQtyService;

	
	@GetMapping()
	@RequiresPermissions("pro:warehouse:warehouse")
	String Warehouse(){
	    return "pro/warehouse/warehouse";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("pro:warehouse:warehouse")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<WarehouseDO> warehouseList = warehouseService.list(query);
		for (WarehouseDO warehouseDO:warehouseList) {
			//查询总金额
			Map<String, Object> params1 = new HashMap<>();
			params1.put("warId",warehouseDO.getId());
			List<ProductQtyDO> productQtyList =productQtyService.list(params1);
			BigDecimal allPurPrice = productQtyList.stream().map(ProductQtyDO::getAllpurPrice).reduce(BigDecimal.ZERO, BigDecimal::add);
			warehouseDO.setAllPurPrice(allPurPrice);
		}
		int total = warehouseService.count(query);
		PageUtils pageUtils = new PageUtils(warehouseList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("pro:warehouse:add")
	String add(){
	    return "pro/warehouse/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("pro:warehouse:edit")
	String edit(@PathVariable("id") Long id,Model model){
		WarehouseDO warehouse = warehouseService.get(id);
		List<DictDO> dictDOS = dictService.listByType("pro_warehouse_type");
		String type = warehouse.getTags();
		for (DictDO dictDO:dictDOS){
			if(type.equals(dictDO.getValue())){
				dictDO.setRemarks("checked");
			}
		}
		model.addAttribute("proWarehouseTypes",dictDOS);
		model.addAttribute("warehouse", warehouse);
	    return "pro/warehouse/edit";
	}

	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("pro:warehouse:add")
	public R save( WarehouseDO warehouse){
		UserDO current = ShiroUtils.getUser();
		warehouse.setGmtModified(new Date());
		warehouse.setUserIdModified(current.getUserId());
		warehouse.setUserNameModified(current.getName());
		if(warehouseService.save(warehouse)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("pro:warehouse:edit")
	public R update( WarehouseDO warehouse){
		UserDO current = ShiroUtils.getUser();
		warehouse.setGmtModified(new Date());
		warehouse.setUserIdModified(current.getUserId());
		warehouse.setUserNameModified(current.getName());
		warehouseService.update(warehouse);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("pro:warehouse:remove")
	public R remove( Long id){
		if(warehouseService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("pro:warehouse:batchRemove")
	public R remove(@RequestParam("ids[]") Long[] ids){
		warehouseService.batchRemove(ids);
		return R.ok();
	}

	/**
	 * 查询所有的仓库
	 * @return
	 */
	@ResponseBody
	@GetMapping("/getalllist")
	public List<WarehouseDO> list(){
		//查询列表数据
		List<WarehouseDO> warehouseDOList = warehouseService.list(null);
		return warehouseDOList;
	}
	
}
